//
//  SEPatientFileViewController.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/17.
//

import UIKit
import DNSPageView

class SEPatientFileViewController: SEBaseViewController {

    var viewModel = SEPatientFileBasicViewModel()

    convenience init(patientId: String) {
        self.init()
        self.viewModel.patientViewModel.model.id = patientId
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "档案详情"
        requestForPatientDetail()
    }
    
    override func setupUI() {
        super.setupUI()
        view.backgroundColor = UIColor.white

        viewModel.updateData = { status in
            switch status {
            case .success:
                self.updateUI()
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.requestForPatientDetail)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    private func updateUI() {
        view.addSubview(pageViewManager.titleView)
        pageViewManager.titleView.setShadow()
        pageViewManager.titleView.snp.makeConstraints { [weak self] (make) in
            guard let self = self else { return }
            make.left.right.equalToSuperview()
            make.top.equalTo(self.fileInfoHeader.snp.bottom)
            make.height.equalTo(50)
        }

        view.addSubview(pageViewManager.contentView)
        pageViewManager.contentView.snp.makeConstraints { (maker) in
            maker.top.equalTo(pageViewManager.titleView.snp.bottom)
            maker.left.right.bottom.equalToSuperview()
        }

        fileInfoHeader.isHidden = false
        fileInfoHeader.updateView(viewModel.patientViewModel)
    }

    @IBOutlet private weak var fileInfoHeader: SEPatientInfoView! {
        didSet {
            fileInfoHeader.isHidden = true
            fileInfoHeader.view.setLayerCorners(rect: fileInfoHeader.bounds, radius: 14, corners: [.topLeft, .topRight])
            fileInfoHeader.view.layer.borderWidth = 1
            fileInfoHeader.view.layer.borderColor = UIColor(hex: "#CFE7F7").withAlphaComponent(0.8).cgColor
            fileInfoHeader.view.layer.masksToBounds = true
            fileInfoHeader.editClosure = {
                self.pushToPatientDetail()
            }
        }
    }
    
    private lazy var pageViewManager: PageViewManager = {
        let style = PageStyle()
        style.isShowBottomLine = true
        style.isTitleScaleEnabled = true
        style.isTitleViewScrollEnabled = true
        style.titleFont = UIFont.regular(14)
        style.titleColor = Asset.textDark.color
        style.titleSelectedColor = Asset.main.color
        style.bottomLineColor = Asset.main.color
        style.bottomLineWidth = 50
        style.bottomLineRadius = 2
        
        let titles = ["基础档案", "健康档案"]

        for i in 0..<titles.count {
            if i == 0 {
                let vc = SEPatientFileBasicViewController(with: viewModel.patientViewModel)
                addChild(vc)
            }
            if i == 1 {
                let vc = SEPatientFileHealthViewController(with: viewModel.patientViewModel)
                addChild(vc)
            }
        }

        return PageViewManager(style: style, titles: titles, childViewControllers: children)
    }()

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

}

extension SEPatientFileViewController {
    private func pushToPatientDetail() {
        let vc = SEPatientDetailViewController(with: viewModel.patientViewModel, editType: .edit)
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension SEPatientFileViewController {
    @objc private func requestForPatientDetail() {
        viewModel.requestPatientDetail()
    }
}

